use reqwest::{Client, Response};
use reqwest::header::HeaderMap;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub  struct ResponseData {
    // 根据你的响应结构定义字段
    message: String,
}

pub struct HttpClient {
    client: Client,
}

impl HttpClient {
    pub fn new() -> Self {
        HttpClient { client: Client::new() }
    }

    pub async fn get(&self, url: &str,   header: Option<HeaderMap>) -> Result<ResponseData, reqwest::Error> {
        let res: Response;
        if header.is_none(){
           res  = self.client.get(url).send().await?.
                error_for_status()?; // 确保响应状态是成功的
        } else {
            let temp = header.unwrap();
            res = self.client.get(url).headers(temp).send().await?.
                error_for_status()?; // 确保响应状态是成功的
        }

        let data: ResponseData = res.json().await?; // 假设返回的是JSON数据
        Ok(data)
    }

    pub async fn post_json(&self, url: &str, body: &serde_json::Value) -> Result<ResponseData, reqwest::Error> {
        let res = self.client.post(url)
            .json(body)
            .send()
            .await?
            .error_for_status()?; // 确保响应状态是成功的
        let data: ResponseData = res.json().await?; // 假设返回的是JSON数据
        Ok(data)
    }
}